home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH11 / EX11_1A.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-13  |  10.8 KB  |  483 lines

  1. ; Ex11_1a.asm
  2. ;
  3. ; Assembly code to link with a C/C++ program.
  4. ; This code directly manipulates the screen giving C++
  5. ; direct access control of the screen.
  6. ;
  7. ; Note: Like PGM11_1.ASM, this code is relatively inefficient.
  8. ; It could be sped up quite a bit using the 80x86 string instructions.
  9. ; However, its inefficiency is actually a plus here since we don't
  10. ; want the C/C++ program (Ex11_1.cpp) running too fast anyway.
  11. ;
  12. ;
  13. ; This code assumes that Ex11_1.cpp is compiled using the LARGE
  14. ; memory model (far procs and far pointers).
  15.  
  16.  
  17.         .xlist
  18.         include     stdlib.a
  19.         includelib    stdlib.lib
  20.         .list
  21.  
  22.         .386            ;Comment out these two statements
  23.                 option  segment:use16   ; if you are not using an 80386.
  24.  
  25.  
  26. ; ScrSeg- This is the video screen's segment address.  It should be
  27. ;        B000 for mono screens and B800 for color screens.
  28.  
  29. ScrSeg        =    0B800h
  30.  
  31.  
  32.  
  33.  
  34.  
  35. _TEXT           segment para public 'CODE'
  36.                 assume  cs:_TEXT
  37.  
  38.  
  39. ; _Capture-      Copies the data on the screen to the array passed
  40. ;        by reference as a parameter.
  41. ;
  42. ; procedure Capture(var ScrCopy:array[0..24,0..79] of word);
  43. ; var x,y:integer;
  44. ; begin
  45. ;
  46. ;    for y := 0 to 24 do
  47. ;        for x := 0 to 79 do
  48. ;        SCREEN[y,x] := ScrCopy[y,x];
  49. ; end;
  50. ;
  51. ;
  52. ; Activation record for Capture:
  53. ;
  54. ;    |            |
  55. ;    | Previous stk contents    |
  56. ;    -------------------------
  57. ;    |  ScrCopy Seg Adrs    |
  58. ;    --               --
  59. ;    | ScrCopy offset Adrs    |
  60. ;    -------------------------
  61. ;       | Return Adrs (offset)  |
  62. ;    -------------------------
  63. ;       |  X coordinate value   |
  64. ;    -------------------------
  65. ;    |  Y coordinate value    |
  66. ;    -------------------------
  67. ;    | Registers, etc.    |
  68. ;    ------------------------- <- SP
  69.  
  70.  
  71.  
  72. ScrCopy_cap     textequ <dword ptr [bp+4]>
  73. X_cap        textequ    <word ptr [bp-2]>
  74. Y_cap        textequ    <word ptr [bp-4]>
  75.  
  76.                 public  _Capture
  77. _Capture        proc    near
  78.         push    bp
  79.         mov    bp, sp
  80.  
  81.         push    es
  82.         push    ds
  83.         push    si
  84.         push    di
  85.         pushf
  86.         cld
  87.  
  88.         mov    si, ScrSeg        ;Set up pointer to SCREEN
  89.         mov    ds, si            ; memory (ScrSeg:0).
  90.         sub    si, si
  91.  
  92.         les    di, ScrCopy_cap        ;Get ptr to capture array.
  93.  
  94.         mov    cx, 1000        ;1000 double words on the screen
  95.     rep    movsd
  96.  
  97.         popf
  98.         pop    di
  99.         pop    si
  100.         pop    ds
  101.         pop    es
  102.         mov    sp, bp
  103.         pop    bp
  104.                 ret     
  105. _Capture        endp
  106.  
  107.  
  108.  
  109.  
  110.  
  111. ; _PutScr-      Copies array passed by reference onto the screen.
  112. ;
  113. ; procedure PutScr(var ScrCopy:array[0..24,0..79] of word);
  114. ; var x,y:integer;
  115. ; begin
  116. ;
  117. ;    for y := 0 to 24 do
  118. ;        for x := 0 to 79 do
  119. ;        ScrCopy[y,x] := SCREEN[y,x];
  120. ; end;
  121. ;
  122. ;
  123. ; Activation record for PutScr:
  124. ;
  125. ;    |            |
  126. ;    | Previous stk contents    |
  127. ;    -------------------------
  128. ;    |  ScrCopy Seg Adrs    |
  129. ;    --               --
  130. ;    | ScrCopy offset Adrs    |
  131. ;    -------------------------
  132. ;       | Return Adrs (offset)  |
  133. ;    -------------------------
  134. ;       |     BP Value          | <- BP
  135. ;    -------------------------
  136. ;       |  X coordinate value   |
  137. ;    -------------------------
  138. ;    |  Y coordinate value    |
  139. ;    -------------------------
  140. ;    | Registers, etc.    |
  141. ;    ------------------------- <- SP
  142.  
  143.  
  144.  
  145. ScrCopy_fill    textequ    <dword ptr [bp+4]>
  146. X_fill        textequ    <word ptr [bp-2]>
  147. Y_fill        textequ    <word ptr [bp-4]>
  148.  
  149.                 public  _PutScr
  150. _PutScr         proc    near
  151.         push    bp
  152.         mov    bp, sp
  153.  
  154.         push    es
  155.         push    ds
  156.         push    si
  157.         push    di
  158.         pushf
  159.         cld
  160.  
  161.         mov    di, ScrSeg        ;Set up pointer to SCREEN
  162.         mov    es, di            ; memory (ScrSeg:0).
  163.         sub    di, di
  164.  
  165.         lds    si, ScrCopy_cap        ;Get ptr to capture array.
  166.  
  167.         mov    cx, 1000        ;1000 double words on the screen
  168.     rep    movsd
  169.  
  170.         popf
  171.         pop    di
  172.         pop    si
  173.         pop    ds
  174.         pop    es
  175.         mov    sp, bp
  176.         pop    bp
  177.                 ret     
  178. _PutScr         endp
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. ; GotoXY-    Positions the cursor at the specified X, Y coordinate.
  187. ;
  188. ; procedure gotoxy(y,x:integer);
  189. ; begin
  190. ;    BIOS(posnCursor,x,y);
  191. ; end;
  192. ;
  193. ; Activation record for GotoXY
  194. ;
  195. ;    |            |
  196. ;    | Previous stk contents    |
  197. ;    -------------------------
  198. ;       |  X coordinate value   |
  199. ;    -------------------------
  200. ;       |  Y coordinate value   |
  201. ;    -------------------------
  202. ;       |  Return Adrs (offset) |
  203. ;    -------------------------
  204. ;    |      Old BP        |
  205. ;    ------------------------- <- BP
  206. ;    | Registers, etc.    |
  207. ;    ------------------------- <- SP
  208.  
  209.  
  210. X_gxy           textequ <byte ptr [bp+6]>
  211. Y_gxy           textequ <byte ptr [bp+4]>
  212.  
  213.                 public  _GotoXY
  214. _GotoXY         proc    near
  215.         push    bp
  216.         mov    bp, sp
  217.  
  218.         mov    ah, 2            ;Magic BIOS value for gotoxy.
  219.         mov    bh, 0            ;Display page zero.
  220.         mov    dh, Y_gxy        ;Set up BIOS (X,Y) parameters.
  221.         mov    dl, X_gxy
  222.         int    10h            ;Make the BIOS call.
  223.  
  224.         mov    sp, bp
  225.         pop    bp
  226.                 ret     
  227. _GotoXY         endp
  228.  
  229.  
  230.  
  231. ; ClrScrn-    Clears the screen and positions the cursor at (0,0).
  232. ;
  233. ; procedure ClrScrn;
  234. ; begin
  235. ;    BIOS(Initialize)
  236. ; end;
  237. ;
  238. ; Activation record for ClrScrn
  239. ;
  240. ;    |            |
  241. ;    | Previous stk contents    |
  242. ;    -------------------------
  243. ;       |  Return Adrs (offset) |
  244. ;    ------------------------- <- SP
  245.  
  246.  
  247.  
  248.                 public  _ClrScrn
  249. _ClrScrn        proc    near
  250.  
  251.         mov    ah, 6        ;Magic BIOS number.
  252.         mov    al, 0        ;Clear entire screen.
  253.         mov    bh, 07        ;Clear with black spaces.
  254.         mov    cx, 0000    ;Upper left corner is (0,0)
  255.         mov    dl, 79        ;Lower X-coordinate
  256.         mov    dh, 24        ;Lower Y-coordinate
  257.         int    10h        ;Make the BIOS call.
  258.  
  259.         push    0        ;Position the cursor to (0,0)
  260.         push    0        ; after the call.
  261.                 call    _GotoXY
  262.                 add     sp, 4           ;Remove parameters from stack.
  263.  
  264.         ret
  265. _ClrScrn        endp
  266.  
  267.  
  268.  
  269.  
  270. ; tstKbd-       Checks to see if a key is available at the keyboard.
  271. ;
  272. ; function tstKbd:boolean;
  273. ; begin
  274. ;       if BIOSKeyAvail then eat key and return true
  275. ;       else return false;
  276. ; end;
  277. ;
  278. ; Activation record for tstKbd
  279. ;
  280. ;    |            |
  281. ;    | Previous stk contents    |
  282. ;    -------------------------
  283. ;       |  Return Adrs (offset) |
  284. ;    ------------------------- <- SP
  285.  
  286.                 public  _tstKbd
  287. _tstKbd         proc    near
  288.                 mov     ah, 1           ;Check to see if key avail.
  289.                 int     16h
  290.                 je      NoKey
  291.                 mov     ah, 0           ;Eat the key if there is one.
  292.                 int     16h
  293.                 mov     ax, 1           ;Return true.
  294.                 ret
  295.  
  296. NoKey:          mov     ax, 0           ;No key, so return false.
  297.                 ret
  298. _tstKbd         endp
  299.  
  300.  
  301.  
  302. ; GetXY- Returns the cursor's current X and Y coordinates.
  303. ;
  304. ; procedure GetXY(var x:integer; var y:integer);
  305. ;
  306. ; Activation record for GetXY
  307. ;
  308. ;    |            |
  309. ;    | Previous stk contents    |
  310. ;    -------------------------
  311. ;       |    Y Coordinate       |
  312. ;       ---    Address        ---
  313. ;       |                       |
  314. ;    -------------------------
  315. ;       |    X coordinate       |
  316. ;       ---    Address        ---
  317. ;       |                       |
  318. ;    -------------------------
  319. ;       |  Return Adrs (offset) |
  320. ;    -------------------------
  321. ;    |      Old BP        |
  322. ;    ------------------------- <- BP
  323. ;    | Registers, etc.    |
  324. ;    ------------------------- <- SP
  325.  
  326.  
  327. GXY_X           textequ <[bp+4]>
  328. GXY_Y           textequ <[bp+8]>
  329.  
  330.                 public  _GetXY
  331. _GetXY          proc    near
  332.                 push    bp
  333.                 mov     bp, sp
  334.                 push    es
  335.  
  336.                 mov     ah, 3           ;Read X, Y coordinates from
  337.                 mov     bh, 0           ; BIOS
  338.                 int     10h
  339.  
  340.                 les     bx, GXY_X
  341.                 mov     es:[bx], dl
  342.                 mov     byte ptr es:[bx+1], 0
  343.  
  344.                 les     bx, GXY_Y
  345.                 mov     es:[bx], dh
  346.                 mov     byte ptr es:[bx+1], 0
  347.  
  348.                 pop     es
  349.                 pop     bp
  350.                 ret
  351. _GetXY          endp
  352.  
  353.  
  354.  
  355. ; PutChar- Outputs a single character to the screen at the current
  356. ;          cursor position.
  357. ;
  358. ; procedure PutChar(ch:char);
  359. ;
  360. ; Activation record for PutChar
  361. ;
  362. ;    |            |
  363. ;    | Previous stk contents    |
  364. ;    -------------------------
  365. ;       | char (in L.O. byte    |
  366. ;    -------------------------
  367. ;       |  Return Adrs (offset) |
  368. ;    -------------------------
  369. ;    |      Old BP        |
  370. ;    ------------------------- <- BP
  371. ;    | Registers, etc.    |
  372. ;    ------------------------- <- SP
  373.  
  374.  
  375. ch_pc           textequ <[bp+4]>
  376.  
  377.                 public  _PutChar
  378. _PutChar        proc    near
  379.                 push    bp
  380.                 mov     bp, sp
  381.  
  382.                 mov     al, ch_pc
  383.                 mov     ah, 0eh
  384.                 int     10h
  385.  
  386.                 pop     bp
  387.                 ret
  388. _PutChar        endp
  389.  
  390.  
  391.  
  392. ; PutStr-  Outputs a string to the display at the current cursor position.
  393. ;          Note that a string is a sequence of characters that ends with
  394. ;          a zero byte.
  395. ;
  396. ; procedure PutStr(var str:string);
  397. ;
  398. ; Activation record for PutStr
  399. ;
  400. ;    |            |
  401. ;    | Previous stk contents    |
  402. ;    -------------------------
  403. ;       |      String           |
  404. ;       ---    Address        ---
  405. ;       |                       |
  406. ;    -------------------------
  407. ;       |  Return Adrs (offset) |
  408. ;    -------------------------
  409. ;    |      Old BP        |
  410. ;    ------------------------- <- BP
  411. ;    | Registers, etc.    |
  412. ;    ------------------------- <- SP
  413.  
  414.  
  415. Str_ps          textequ <[bp+4]>
  416.  
  417.                 public  _PutStr
  418. _PutStr         proc    near
  419.                 push    bp
  420.                 mov     bp, sp
  421.                 push    es
  422.  
  423.                 les     bx, Str_ps
  424. PS_Loop:        mov     al, es:[bx]
  425.                 cmp     al, 0
  426.                 je      PC_Done
  427.  
  428.                 push    ax
  429.                 call    _PutChar
  430.                 pop     ax
  431.                 inc     bx
  432.                 jmp     PS_Loop
  433.  
  434. PC_Done:        pop     es
  435.                 pop     bp
  436.                 ret
  437. _PutStr         endp
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445. ; StartPgm-     This is where DOS starts running the program.  This is
  446. ;               a substitute for the C0L.OBJ file normally linked in by
  447. ;               the Borland C++ compiler.  This code provides this
  448. ;               routine to avoid legal problems (i.e., distributing
  449. ;               unlinked Borland libraries).  You can safely ignore
  450. ;               this code.  Note that the C++ main program is a near
  451. ;               procedure, so this code needs to be in the _TEXT segment.
  452.  
  453.                 extern  _main:near
  454. StartPgm        proc    near
  455.  
  456.                 mov     ax, _DATA
  457.                 mov     ds, ax
  458.                 mov     es, ax
  459.                 mov     ss, ax
  460.                 lea     sp, EndStk
  461.  
  462.                 call    near ptr _main
  463.                 mov     ah, 4ch
  464.                 int     21h
  465. StartPgm        endp
  466.  
  467.  
  468. _TEXT           ends
  469.  
  470.  
  471. _DATA           segment word public "DATA"
  472. stack           word    1000h dup (?)
  473. EndStk          word    ?
  474.  
  475. _DATA           ends
  476.  
  477. sseg            segment para stack 'STACK'
  478.                 word    1000h dup (?)
  479. sseg            ends
  480.  
  481.  
  482.                 end     StartPgm
  483.